package com.ynet.middleground.user.model;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ynet.core.common.PagerInfo;
import com.ynet.core.exception.BusinessException;
import com.ynet.middleground.user.bean.CertMixQueryReqPager;
import com.ynet.middleground.user.common.ErrorMessageConstant;
import com.ynet.middleground.user.common.SystemConstant;
import com.ynet.middleground.user.dao.*;
import com.ynet.middleground.user.dto.CertMixInfoDTO;
import com.ynet.middleground.user.dto.UserCfcaInfoDTO;
import com.ynet.middleground.user.dto.UserEsignInfoDTO;
import com.ynet.middleground.user.entity.*;

/**
 * @author liulx
 * @description 证书综合处理类
 * @date 2020-01-06 17:00
 */
@Component
public class CertModel {

    @Autowired
    CertMapper certMapper;

    @Autowired
    private Mapper dozerBeanMapper;

    @Autowired
    EnterpriseBasicInfoMapper enterpriseBasicInfoMapper;

    @Autowired
    EnterpriseUserInfoMapper enterpriseUserInfoMapper;

    @Autowired
    OrganizationalStructureMapper organizationalStructureMapper;

    @Autowired
    EmployeeInformationMapper employeeInformationMapper;

    @Autowired
    NaturalPersonBaseInformationMapper naturalPersonBaseInformationMapper;

    /**
     * 查询证书列表
     * 
     * @param queryReq 证书混合查询请求对象
     * @return 证书列表
     */
    public List<CertMixInfoDTO> listCert(CertMixQueryReqPager queryReq) {
        List<CertMixInfoDTO> resultList = new ArrayList<>();
        PagerInfo pagerInfo = queryReq.getPagerInfo();
        // 计算应该从第几条记录去取
        int start = (pagerInfo.getPageNum() - 1) * pagerInfo.getPageSize();
        Integer userId = queryReq.getUserId();
        // if (3 < queryReq.getUserType() || 0 > queryReq.getUserType()) {
        // throw new BusinessException(ErrorMessageConstant.ECUC0045_MESSAGE, ErrorMessageConstant.ECUC0045_CODE);
        // }
        // // 0: 客户方 1: 资金方，只能查询自己
        // if (0 == queryReq.getUserType() || 1 == queryReq.getUserType()) {
        // if (null == queryReq.getOperationUserId()){
        // throw new BusinessException(ErrorMessageConstant.ECUC0047_MESSAGE, ErrorMessageConstant.ECUC0047_CODE);
        // }
        // userId = queryReq.getOperationUserId().toString();
        // }

        // 1 查询 e签宝
        if (1 == queryReq.getCertType()) {
            List<UserEsignInfoDTO> esignList = certMapper.listCertEsign(queryReq, userId, start);
            esignList.stream().forEach(ue -> {
                CertMixInfoDTO mixInfoDTO = new CertMixInfoDTO();
                UserEsignInfoDTO esign = new UserEsignInfoDTO();
                esign.setAccountId(ue.getAccountId());
                esign.setFingerprint(ue.getFingerprint());
                esign.setEsignChannel(ue.getEsignChannel());
                esign.setEsignCreateBy(ue.getEsignCreateBy());
                esign.setEsignGmtCreate(ue.getEsignGmtCreate());
                esign.setEsignGmtModified(ue.getEsignGmtModified());
                esign.setEsignModifiedBy(ue.getEsignModifiedBy());
                mixInfoDTO.setUserEsignInfoDTO(esign);
                if (SystemConstant.USER_TYPE_0.equals(ue.getUserType())) {
                    LambdaQueryWrapper<EnterpriseBasicInfo> queryEB = new LambdaQueryWrapper<EnterpriseBasicInfo>();
                    queryEB.eq(EnterpriseBasicInfo::getUserId, ue.getUserId());
                    EnterpriseBasicInfo user = enterpriseBasicInfoMapper.selectOne(queryEB);
                    dozerBeanMapper.map(user, esign);
                    esign.setUserType(SystemConstant.USER_TYPE_0);
                    mixInfoDTO.setUserEsignInfoDTO(esign);
                } else if (SystemConstant.USER_TYPE_1.equals(ue.getUserType())) {
                    LambdaQueryWrapper<EmployeeInformation> queryEB = new LambdaQueryWrapper<EmployeeInformation>();
                    queryEB.eq(EmployeeInformation::getUserId, ue.getUserId());
                    queryEB.eq(EmployeeInformation::getIsDelete, SystemConstant.IS_DELETE_0);
                    List<EmployeeInformation> users = employeeInformationMapper.selectList(queryEB);
                    dozerBeanMapper.map(users.get(0), esign);
                    esign.setUserType(SystemConstant.USER_TYPE_1);
                    mixInfoDTO.setUserEsignInfoDTO(esign);
                } else if (SystemConstant.USER_TYPE_2.equals(ue.getUserType())) {
                    LambdaQueryWrapper<NaturalPersonBaseInformation> queryEB =
                        new LambdaQueryWrapper<NaturalPersonBaseInformation>();
                    queryEB.eq(NaturalPersonBaseInformation::getUserId, ue.getUserId());
                    NaturalPersonBaseInformation user = naturalPersonBaseInformationMapper.selectOne(queryEB);
                    dozerBeanMapper.map(user, esign);
                    esign.setUserType(SystemConstant.USER_TYPE_2);
                    mixInfoDTO.setUserEsignInfoDTO(esign);
                } else if (SystemConstant.USER_TYPE_3.equals(ue.getUserType())) {
                    LambdaQueryWrapper<EnterpriseUserInfo> queryEB = new LambdaQueryWrapper<EnterpriseUserInfo>();
                    queryEB.eq(EnterpriseUserInfo::getUserId, ue.getUserId());
                    queryEB.eq(EnterpriseUserInfo::getIsDelete, SystemConstant.IS_DELETE_0);
                    List<EnterpriseUserInfo> users = enterpriseUserInfoMapper.selectList(queryEB);
                    dozerBeanMapper.map(users.get(0), esign);
                    esign.setUserType(SystemConstant.USER_TYPE_3);
                    mixInfoDTO.setUserEsignInfoDTO(esign);
                } else if (SystemConstant.USER_TYPE_4.equals(ue.getUserType())) {
                    LambdaQueryWrapper<OrganizationalStructure> queryEB =
                        new LambdaQueryWrapper<OrganizationalStructure>();
                    queryEB.eq(OrganizationalStructure::getOrganizationalId, ue.getUserId());
                    OrganizationalStructure user = organizationalStructureMapper.selectOne(queryEB);
                    dozerBeanMapper.map(user, esign);
                    esign.setUserType(SystemConstant.USER_TYPE_4);
                    esign.setUserId(user.getOrganizationalId());
                    esign.setUserName(user.getOrganizationalName());
                    mixInfoDTO.setUserEsignInfoDTO(esign);
                }
                resultList.add(mixInfoDTO);
            });
            pagerInfo.setTotal(certMapper.countCertEsign(queryReq, userId));
            return resultList;
        } else if (0 == queryReq.getCertType()) {
            List<UserCfcaInfoDTO> cfcaList = certMapper.listCertCfca(queryReq, userId, start);
            cfcaList.stream().forEach(uc -> {
                CertMixInfoDTO mixInfoDTO = new CertMixInfoDTO();
                if (SystemConstant.USER_TYPE_0.equals(uc.getUserType())) {
                    LambdaQueryWrapper<EnterpriseBasicInfo> queryEB = new LambdaQueryWrapper<EnterpriseBasicInfo>();
                    queryEB.eq(EnterpriseBasicInfo::getUserId, uc.getUserId());
                    EnterpriseBasicInfo user = enterpriseBasicInfoMapper.selectOne(queryEB);
                    UserCfcaInfoDTO cert = dozerBeanMapper.map(user, UserCfcaInfoDTO.class);
                    cert.setUserType(SystemConstant.USER_TYPE_0);
                    cert.setUKeyId(uc.getUKeyId());
                    cert.setDn(uc.getDn());
                    cert.setSequenceNo(uc.getSequenceNo());
                    cert.setSerialNo(uc.getSerialNo());
                    cert.setAuthCode(uc.getAuthCode());
                    cert.setCertStatus(uc.getCertStatus());
                    cert.setStartTime(uc.getStartTime());
                    cert.setEndTime(uc.getEndTime());
                    cert.setSignatureCert(uc.getSignatureCert());
                    cert.setCfcaCreateBy(uc.getCfcaCreateBy());
                    cert.setCfcaGmtCreate(uc.getCfcaGmtCreate());
                    cert.setCfcaModifiedBy(uc.getCfcaModifiedBy());
                    cert.setCfcaGmtModified(uc.getCfcaGmtModified());
                    mixInfoDTO.setUserCfcaInfoDTO(cert);
                } else if (SystemConstant.USER_TYPE_4.equals(uc.getUserType())) {
                    LambdaQueryWrapper<OrganizationalStructure> queryEB =
                        new LambdaQueryWrapper<OrganizationalStructure>();
                    queryEB.eq(OrganizationalStructure::getOrganizationalId, uc.getUserId());
                    OrganizationalStructure user = organizationalStructureMapper.selectOne(queryEB);
                    UserCfcaInfoDTO cert = dozerBeanMapper.map(user, UserCfcaInfoDTO.class);
                    cert.setUserType(SystemConstant.USER_TYPE_4);
                    cert.setUserId(user.getOrganizationalId());
                    cert.setUserName(user.getOrganizationalName());
                    cert.setUKeyId(uc.getUKeyId());
                    cert.setDn(uc.getDn());
                    cert.setSequenceNo(uc.getSequenceNo());
                    cert.setSerialNo(uc.getSerialNo());
                    cert.setAuthCode(uc.getAuthCode());
                    cert.setCertStatus(uc.getCertStatus());
                    cert.setStartTime(uc.getStartTime());
                    cert.setEndTime(uc.getEndTime());
                    cert.setSignatureCert(uc.getSignatureCert());
                    cert.setCfcaCreateBy(uc.getCfcaCreateBy());
                    cert.setCfcaGmtCreate(uc.getCfcaGmtCreate());
                    cert.setCfcaModifiedBy(uc.getCfcaModifiedBy());
                    cert.setCfcaGmtModified(uc.getCfcaGmtModified());
                    mixInfoDTO.setUserCfcaInfoDTO(cert);
                }else{
                    UserCfcaInfoDTO cert = new UserCfcaInfoDTO();
                    cert.setUserType(SystemConstant.USER_TYPE_4);
                    cert.setUKeyId(uc.getUKeyId());
                    cert.setDn(uc.getDn());
                    cert.setSequenceNo(uc.getSequenceNo());
                    cert.setSerialNo(uc.getSerialNo());
                    cert.setAuthCode(uc.getAuthCode());
                    cert.setCertStatus(uc.getCertStatus());
                    cert.setStartTime(uc.getStartTime());
                    cert.setEndTime(uc.getEndTime());
                    cert.setSignatureCert(uc.getSignatureCert());
                    cert.setCfcaCreateBy(uc.getCfcaCreateBy());
                    cert.setCfcaGmtCreate(uc.getCfcaGmtCreate());
                    cert.setCfcaModifiedBy(uc.getCfcaModifiedBy());
                    cert.setCfcaGmtModified(uc.getCfcaGmtModified());
                    mixInfoDTO.setUserCfcaInfoDTO(cert);
                }
                resultList.add(mixInfoDTO);
            });
            pagerInfo.setTotal(certMapper.countCertCfca(queryReq, userId));
            return resultList;
        }
        throw new BusinessException(ErrorMessageConstant.ECUC0046_MESSAGE, ErrorMessageConstant.ECUC0046_CODE);
    }

}
